/******************************************************************************
 * @File name   :      MonitorLoginController.java
 *
 * @Author      :      Cai Cai
 *
 * @Date        :      2012-4-17
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2012-4-17 下午05:13:02        Cai Cai     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.framework.workbench.userlogin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cap.bts.framework.auth.util.UserAuthenticationUtils;
import com.cap.bts.framework.workbench.userlogin.dto.LoginDto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.usermgnt.constant.UserType;
import com.cap.bts.framework.usermgnt.domain.User;
import com.cap.bts.framework.usermgnt.service.IUserMgntService;
import com.cap.bts.framework.usermgnt.util.PasswordUtil;

@Controller
@RequestMapping(value = "/monitorLogin")
public class MonitorLoginController {
    
    @Autowired
    private IUserMgntService userService;
    
    /**
     * 
     * @Date        :      2012-4-17
     * @param model
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String login(Model model) {
        model.addAttribute("loginDto", new LoginDto());
        return "workbench/monitor_login";
    }
    /**
     * 
     * @Date        :      2012-4-17
     * @param loginDto
     * @param result
     * @param model
     * @param request
     * @param response
     * @return
     * @throws AppException
     */
    @RequestMapping(method = RequestMethod.POST)
    public String processLogin(LoginDto loginDto, BindingResult result,
            Model model, HttpServletRequest request, HttpServletResponse response)throws AppException {
        User actualUser = userService.getUserByUserCode(loginDto.getUserCode());
        if(actualUser==null){
            return "workbench/monitor_login";
        }
        if (actualUser.getUserType() != UserType.CENTRAL) {//只能总部端用户登录
            return "workbench/monitor_login";
        }
        String decryptedPwd = PasswordUtil.decodePassword(actualUser.getPassword());
        if (!decryptedPwd.equals(loginDto.getPasswd())) {
        	return "workbench/monitor_login";
        }
        UserAuthenticationUtils.setLoginToCookie(request, response, actualUser
            .getUserCode());
        UserAuthenticationUtils.setSessionIdToCookie(request, response);
        request.getSession(true).setAttribute("userSession", actualUser);
        return "common/monitor_index";
//        return "common/job_jms_monitor";
    }
}
